package me.jiatao.wan.init

import android.app.Application
import android.content.Context
import androidx.startup.Initializer
import coil.Coil
import me.jiatao.wan.BuildConfig
import me.jiatao.wan.di.GlobalEntryPoint
import timber.log.Timber
import com.tencent.mmkv.MMKV
import me.jiatao.eventbus.EventBusInitializer
import me.jiatao.eventbus.util.ILogger
import java.util.logging.Level


/**
 * @Author: taojia
 * @CreateDate: 2021/8/24 3:19 下午
 * @Version: 1.0
 * @Description: 初始化器
 */

class AppInitializer : Initializer<Unit> {
    /**
     * 初始化Coil加载器
     */
    private fun initCoil(context: Context) {
        /** 注入ImageLoader实例 */
        val imageLoader = GlobalEntryPoint.resolveApplication(context).injectImageLoader()
        Coil.setImageLoader(imageLoader)
    }

    /**
     * 初始化EventBus
     */
    private fun initEventBus(context: Context) {
        EventBusInitializer.init(context as Application, object : ILogger {
            override fun log(level: Level, msg: String) {
                Timber.d(msg)
            }

            override fun log(level: Level, msg: String, th: Throwable) {
                Timber.e(th, msg)
            }
        })
    }

    override fun create(context: Context) {
        AppHelper.init(context)
        initCoil(context)
        initEventBus(context)

        if (!BuildConfig.DEBUG) {
            return
        }
        // StrictMode.setThreadPolicy(
        //     StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()
        // )
        // StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build())
        Timber.plant(Timber.DebugTree())
    }

    override fun dependencies(): MutableList<Class<out Initializer<*>>> = mutableListOf()
}